import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)
// Login组件
import login from '@/components/Login/Login.vue'
// home
import home from '@/components/Home/Home.vue'
// home 二级路由 
import hello from '@/components/Home/Hello.vue'
import users from '@/components/Home/user/Users.vue'
import rights from '@/components/Home/power/rights.vue'
import roles from '@/components/Home/power/roles.vue'
import cate from '@/components/Home/goods/cate.vue'
import params from '@/components/Home/goods/params.vue'
import goods from '@/components/Home/goods/goods.vue'
import add from '@/components/Home/goods/add.vue'
import orders from '@/components/Home/orders/orders.vue'
import report from '@/components/Home/report/report.vue'
const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: login },
    {
      path: '/home',
      component: home,
      redirect: '/hello',
      children: [
        { path: '/hello', component: hello },
        { path: '/users', component: users },
        { path: '/rights', component: rights },
        { path: '/roles', component: roles },
        { path: '/categories', component: cate },
        { path: '/params', component: params },
        { path: '/goods', component: goods },
        { path: '/add_Goods', component: add },
        { path: '/orders', component: orders },
        { path: '/reports', component: report },
      ]
    }
  ]
})
// 导航首位
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // form 代表从哪路径而来
  // next() 放行 next('Login') 强制跳转
  if (to.path === '/login') return next();
  const tokenStr = window.sessionStorage.getItem('token');
  // 判断token 如果没有就强制跳转
  if (!tokenStr) return next('/login')
  // 否则放行
  next()
})

export default router